/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Command;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import entidade.AppHelper;
import entidade.Cidade;
import entidade.MobCatController;
import exceptions.ReturnWsException;
import form.components.CidadeListRenderer;

/**
 *
 * @author victorinox
 */
public class CidadeForm extends AbstractForm {

    private List list;
    private final AbstractForm prevForm;
    private CategoriasRaizForm nextForm;

    public CidadeForm(AbstractForm returnForm) {
        super("Escolha a cidade");
        this.prevForm = returnForm;
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addCommand(new Command("Voltar") {

            public void actionPerformed(ActionEvent evt) {

                prevForm.show();
            }
        });
    }

    private void listAction() {
        Cidade cit = (Cidade) list.getSelectedItem();
        AppHelper.getInstance().cidadeSelecionada = cit.id;
        if (nextForm == null) {
            nextForm = new CategoriasRaizForm();
        }
        nextForm.show();
    }

    public void show(int estadoId) {
        if (list != null) {
            removeComponent(list);
        }
        try {
            list = new List(MobCatController.getInstance().listCidades(estadoId));
        } catch (ReturnWsException ex) {
            ex.showError(prevForm);
        }
        list.setListCellRenderer(new CidadeListRenderer());
        addComponent(list);
        list.setSmoothScrolling(true);
        list.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                WaitForm.getInstance().show();
                new Thread(new Runnable() {

                    public void run() {
                        listAction();
                    }
                }).start();

            }
        });
        this.show();
    }

    public void show() {
        WaitForm.getInstance().setVisible(false);
        super.show();
    }
}
